import mongoose from 'mongoose'
import config from './index.js'

mongoose.connect(config.url, {
  auto_reconnect: true,
  poolSize: 10,
  useNewUrlParser: false,
  useUnifiedTopology: false
})

mongoose.Promise = global.Promise

const db = mongoose.connection

db.once('open', () => {
  console.log(`MongoDB Bingo ==> ${db.name}`)
})

db.on('error', function(error) {
  console.error('Error in MongoDb connection: ' + error)
  mongoose.disconnect()
})

db.on('close', function() {
  console.log('数据库断开，重新连接数据库')
  mongoose.connect(config.url, { server: { auto_reconnect: true } })
})

// 过期警告
mongoose.set('useFindAndModify', false)
